define(function(require, exports, module) {
    // require('./plug/hammer.min');
    // require('./plug/layer/layer');
    // require('./aes');
    // require('./shop');
    var myPub = require('./public');

    /*********************  账号和快捷登陆切换  ****************************/
    var oLoginNavLi = $('.js_loginNav').find('li'); 	//登陆导航
    var oLoginBoxList = $('.js_loginBoxList'); 			//账号和快捷登陆区域

    var iNowNavLi = 0;
    oLoginBoxList.eq(0).addClass('js_inputBoxList');
    oLoginNavLi.hammer().on('tap', function() {
        var index = $(this).index();
        oLoginNavLi.removeClass('active').eq(index).addClass('active');
        oLoginBoxList.hide().removeClass('js_inputBoxList').eq(index).show().addClass('js_inputBoxList');
        iNowNavLi = index
        var timestamp = (new Date()).valueOf();
        var imageSrc = oGetCodeImg.find('img').attr("src");

        if(imageSrc.indexOf("?") >= 0) {
            imageSrc = imageSrc.substring(0, imageSrc.indexOf("?"));
        }
        imageSrc = imageSrc + "?timestamp=" + timestamp;
        oGetCodeImg.find('img').attr("src", imageSrc);
    });




    /******************   账号验证 start        ***********************/
    var oLoginPhoneNum = $('.js_loginPhoneNum'); 	//手机号码框
    var oLoginPhoneNumQuick = $('.js_loginPhoneNum_quick');	//快捷登录手机输入框
    var oGetCode = $('.js_getCode');                    //验证码按钮
    var oGetCodeDisabled = $('.js_getCodeDisabled'); //验证码倒计时
    var oClearBtn = $('.js_clearBtn'); 				//清理按钮
    var oShowPwd = $('.js_showPwd'); 				//显示和隐藏密码
    var oLoginPassWord = $('.js_loginPassWord'); 	//密码输入

    var oYzmInput = $('.js_yzm'); 					//验证码输入框
    var oPicYzm = $('.js_picYzm');					//图形验证码
    var oImgCode = $('.js_imgCode'); //图片验证输入框
    var oCode = $('.js_code');//注册页面验证码
    var offPicYzm = false;
    if(oPicYzm.css('display') == 'none' || oPicYzm.length == 0) {
        offPicYzm = true;
        //移除图形验证码
        oPicYzm.remove();
    }


    //清除按钮操作
    oClearBtn.hammer().on('tap', function() {
        $(this).parents('.js_loginMob').removeClass('has_error').removeClass('has_success');
    });

    //验证码显示清除按钮
    oYzmInput.on('input propertychange', function() {
        var _oClearBtn = $(this).siblings('.js_clearBtn');
        $(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
    });

    //密码显示和隐藏
    oShowPwd.hammer().on('tap', function() {
        if($(this).hasClass('on')) {
            $(this).removeClass('on icon-eye_look').addClass('icon-eye_close').siblings('.js_loginPassWord').attr('type', 'password')
        } else {
            $(this).addClass('on icon-eye_look').removeClass('icon-eye_close').siblings('.js_loginPassWord').attr('type', 'text')
        }

    });

    //快捷登陆手机验证
    oLoginPhoneNumQuick.on('input propertychange', function() {
        phonemobile(oLoginPhoneNumQuick);
        var _oClearBtn = $(this).siblings('.js_clearBtn');

        $(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
    });

    //手机输入框
    oLoginPhoneNum.on('input propertychange', function() {
        phonemobile(oLoginPhoneNum);
        var _oClearBtn = $(this).siblings('.js_clearBtn');

        $(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
    });

    var flag = false;//是否提示过
    var bindTap = true;     // 重复点击
    //发送验证码
    $("#codeTime").hammer().on('tap', function() {
        var oLoginMob = oLoginPhoneNum.parents('.js_loginMob');

        if(bindTap){
            bindTap = false;
            if(oLoginPhoneNum.val() == '') {
                myPub.TipLayer('请输入手机号！');
                bindTap = true;
            } else if(oLoginMob.hasClass('has_error')) {
                myPub.TipLayer('请输入正确手机号！');
                bindTap = true;
            } else {
                var phonev = $("#phone").val();
                if(flag){


                    G_JSON_POST('/member/sendCode2',{"mobile":phonev},function(data){
                        if(data.status == "success"){
                            setCode($("#codeTime"));
                            bindTap = true;
                        }else{
                            myPub.TipLayer("验证码发送失败");
                            bindTap = true;
                        }
                    });
                }else{

                    G_JSON_POST('/member/sendCode',{"mobile":phonev},function(data){
                        if(data.status == "01"){
                            flag = true;
                            myPub.TipBtnLayer(data.msg,'提示');
                            bindTap = true;
                        }else if(data.status == "02"){
                            myPub.TipLayer(data.msg);
                            bindTap = true;
                        }else{
                            setCode($("#codeTime"));
                            bindTap = true;
                        }
                    });
                }
            }
        }

    });



    //快捷登陆获取验证码
    $('#codeTime2').hammer().on('tap', function() {
        var oLoginMobQuick = oLoginPhoneNumQuick.parents('.js_loginMob');
        if(oLoginPhoneNumQuick.val() == '') {
            myPub.TipLayer('请输入手机号！');
        } else if(oLoginMobQuick.hasClass('has_error')) {
            myPub.TipLayer('请输入正确手机号！');
        } else if(oImgCode.val() == '') {
            myPub.TipLayer('请输入图片中的验证码！');
        }
        else {
            sendCode({"mobile": oLoginPhoneNumQuick.val(),"type":2, "captch": Encrypt(oImgCode.val())},$('#codeTime2'));
        }
    });

    //注册 找回密码发送验证码
    $('#codeTime3').hammer().on('tap', function() {
        var oLoginMob = oLoginPhoneNum.parents('.js_loginMob');
        if(oLoginPhoneNum.val() == '') {
            myPub.TipLayer('请输入手机号！');
        } else if(oLoginMob.hasClass('has_error')) {
            myPub.TipLayer('请输入正确手机号！');
        }
        else if(oImgCode.val() == '') {
            myPub.TipLayer('请输入图片中的验证码！');
        }
        else {
            sendCode({"mobile": oLoginPhoneNum.val(),"type":1, "captch": Encrypt(oCode.val())},$('#codeTime3'));
        }
    });

    //找回密码发送验证码
    $('#codeTime4').hammer().on('tap', function() {
        var oLoginMob = oLoginPhoneNum.parents('.js_loginMob');
        if(oLoginPhoneNum.val() == '') {
            myPub.TipLayer('请输入手机号！');
        } else if(oLoginMob.hasClass('has_error')) {
            myPub.TipLayer('请输入正确手机号！');
        }
        else if(oImgCode.val() == '') {
            myPub.TipLayer('请输入图片中的验证码！');
        }
        else {
            sendCode({"mobile": oLoginPhoneNum.val(),"type":0, "captch": Encrypt($('.js_zhaohuiCode').val())},$('#codeTime4'));
        }
    });

    //点击切换图片验证码
    var oGetCodeImg = $('.js_getCodeImg');
    oGetCodeImg.hammer().on('tap',function() {
        var timestamp = (new Date()).valueOf();
        var imageSrc = oGetCodeImg.find('img').attr("src");

        if(imageSrc.indexOf("?") >= 0) {
            imageSrc = imageSrc.substring(0, imageSrc.indexOf("?"));
        }
        imageSrc = imageSrc + "?timestamp=" + timestamp;
        oGetCodeImg.find('img').attr("src", imageSrc);
    });

    //是否验证密保问题
    var ischeckAnwser = false;
    //发送验证码
    function sendCode(datas,obj) {
        $.ajax({
            url: '/ddjLoginAuth/sendCode',
            data: datas,
            type: 'POST',
            dataType: 'json',
            success: function(data) {
                if(data.code == 'S001') {
                    setCode(obj);
                    myPub.TipLayer(data.msg);
                    if($("#answerdiv").length>0){
                        $.ajax({
                            url: '/ddjLoginAuth/checkAnwser',
                            data: {'mobile':datas["mobile"]},
                            type: 'POST',
                            dataType: 'json',
                            success: function(data){
                                if(data.status=== '01'){
                                    $("#answerdiv").show();
                                    ischeckAnwser = true;
                                }
                            }
                        });
                    }

                }
                else if(data.code == 'F_001' || data.code == 'F002') {
                    refreshCode();
                    myPub.TipLayer(data.msg);
                }
                else{
                    if(datas['type'] == 1){
                        //注册
                        if(data.code === 'F_003'){
                            //手机已注册
                            myPub.TipBtnLayer(
                                '<div class="layerListInfo"><h4 class="layerList_H4"><p>注册失败!</p><a>'+data.msg+'</a></h4>'+
                                '<p class="layerList_p"><a class="color_href" href="/ddjLoginAuth/goLogin?m='+ datas["mobile"] +'">登录</a></p>'+
                                '<p class="layerList_p"><a class="color_href" href="/ddjLoginAuth/goRetrieve?m='+ datas["mobile"] +'">找回密码</a></div>',
                                'no','取消',function(){myPub.LayerCloseAll();});
                        }
                    }else{
                        if(data.code === 'F_004'){
                            //找回
                            myPub.TipBtnLayer(
                                '<div class="layerListInfo"><h4 class="layerList_H4"><p>找回失败!</p><a>'+data.msg+'</a></h4>'+
                                '<p class="layerList_p"><a class="color_href" href="/ddjLoginAuth/goRegister?m='+ datas["mobile"] +'">注册</a></div>',
                                'no','取消',function(){myPub.LayerCloseAll();});
                        }

                    }
                }

            },
            error:function(x){
                if(403==x.status){
                    window.location.href='/loginExpired';
                }
                console.log(x.status);
            }
        });
    }


    //密码输入检测
    oLoginPassWord.each(function(i) {
        oLoginPassWord.eq(i).on('input propertychange', function() {
            userPassWord(oLoginPassWord.eq(i));

            var _oClearBtn = $(this).siblings('.js_clearBtn');
            $(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
        });

    });

    /*验证手机号码*/
    function phonemobile(obj) {

        var oLoginMob = obj.parents('.js_loginMob');
        myPub.checkMobile(obj, function() {
            oLoginMob.removeClass('has_error').removeClass('has_success');
            //myPub.TipLayer('电话号码不能为空');
        }, function() {
            oLoginMob.addClass('has_error').removeClass('has_success');
            //myPub.TipLayer('不是手机号码,请重新谈填写');

        }, function() {
            oLoginMob.addClass('has_success').removeClass('has_error');

        });
    }

    /*验证密码*/
    function userPassWord(obj) {
        var oLoginMob = obj.parents('.js_loginMob');
        myPub.checkPassWord(obj, function() {
            oLoginMob.removeClass('has_error').removeClass('has_success');
        }, function() {
            oLoginMob.addClass('has_error').removeClass('has_success');

        }, function() {
            oLoginMob.addClass('has_success').removeClass('has_error');

        });
    }

    /*倒计时*/
    function setCode(obj) {
        obj.hide();
        oGetCodeDisabled.show();
        myPub.CutTime(60, function() {
            oGetCodeDisabled.hide();
            obj.show().text('重新发送');
        }, function(time) {
            oGetCodeDisabled.text(time + '秒');
        });
    }

    //刷新验证码
    function refreshCode(){
        var oGetCodeImg = $('.js_getCodeImg');
        var timestamp = (new Date()).valueOf();
        var imageSrc = oGetCodeImg.find('img').attr("src");

        if(imageSrc.indexOf("?") >= 0) {
            imageSrc = imageSrc.substring(0, imageSrc.indexOf("?"));
        }
        imageSrc = imageSrc + "?timestamp=" + timestamp;
        oGetCodeImg.find('img').attr("src", imageSrc);
    }
    /******************   账号验证 end        ***********************/

        //登录操作
    var oLoginSubmit = $('.js_loginSubmit'); //登录

    oLoginSubmit.hammer().on('tap', function() {
        var oInputBoxList=  $('.js_inputBoxList')
        //if(iNowNavLi == 1) {
        if(oInputBoxList.hasClass('accLogin')) {

            SublimtBefor(function(index) {
                switch(index) {
                    case 0:
                        myPub.TipLayer('手机号码不能为空');
                        break;
                    case 1:
                        myPub.TipLayer('密码不能为空');
                        break;
                    case 2:
                        myPub.TipLayer('图形验证码不能为空');
                        break;
                }
            }, function(index) {
                switch(index) {
                    case 0:
                        myPub.TipLayer('请输入正确的手机号码');
                        break;
                    case 1:
                        myPub.TipLayer('请输入正确格式的密码');
                        break;
                }
            }, function() {
                $.ajax({
                    url: '/ddjLoginAuth/login',
                    data: {
                        "mobile": $('.js_loginPhoneNum').val(),
                        "password":$('#password').val(),
                        "captch" : Encrypt($('#verifyCode2').val()),
                        "url": $('.js_url').val()
                    },
                    type: 'POST',
                    dataType: 'json',
                    success: function(data) {
                        if(data.code == 'S_000') {
                            myPub.TipLayer(data.msg);
                            var lastUrl = getCookie("historyLastUrl");
                            if(lastUrl !=undefined && lastUrl.length>0){
                                var url = lastUrl.replace(/"/g,'');
                                window.location.href=url;
                            }else{
                                window.location.href="/home";
                            }
                        }else if(data.code == 'S_111'){
                            window.location.href=data.msg;
                        }else {
                            if(data.code == 'F_004'){
                                //找回
                                myPub.TipBtnLayer(
                                    '<div class="layerListInfo"><h4 class="layerList_H4"><p>登入失败!</p><a>'+data.msg+'</a></h4>'+
                                    '<p class="layerList_p"><a class="color_href" href="/ddjLoginAuth/goRetrieve?m='+ $('.js_loginPhoneNum').val() +'">找回密码</a></div>',
                                    'no','取消',function(){myPub.LayerCloseAll();});
                            }else{
                                refreshCode();
                                myPub.TipLayer(data.msg);
                            }
                        }
                    },
                    error:function(x){
                        if(403==x.status){
                            window.location.href="/loginExpired";
                        }
                        console.log(x.status);
                    }
                });
            })
        }
        //else if(iNowNavLi == 0) {
        else if(oInputBoxList.hasClass('mobLogin')) {
            //是否隐藏图形验证码
            if(offPicYzm) {
                SublimtBefor(function(index) {
                    switch(index) {
                        case 0:
                            myPub.TipLayer('手机号码不能为空');
                            break;
                        case 1:
                            myPub.TipLayer('验证码不能为空');
                            break;
                    }
                }, function(index) {
                    switch(index) {
                        case 0:
                            myPub.TipLayer('请输入正确的手机号码');
                            break;

                    }
                }, function() {
                    console.log('完成！')
                })
            }
            else {
                SublimtBefor(function(index) {
                    switch(index) {
                        case 0:
                            myPub.TipLayer('手机号码不能为空');
                            break;
                        case 1:
                            myPub.TipLayer('右侧验证码不能为空');
                            break;
                        case 2:
                            myPub.TipLayer('验证码不能为空');
                            break;
                    }
                }, function(index) {
                    switch(index) {
                        case 0:
                            myPub.TipLayer('请输入正确的手机号码');
                            break;

                    }
                }, function() {
                    $.ajax({
                        url: '/ddjLoginAuth/quickLogin',
                        data: {
                            "mobile": $('.js_inputBoxList').find('.js_loginPhoneNum_quick').val(),
                            "smsCode": $('.js_inputBoxList').find('.js_msyzm').val(),
                            "captch" : Encrypt($('#verifyCode').val()),
                            "url":$('.js_url').val()
                        },
                        type: 'POST',
                        dataType: 'json',
                        success: function(data) {
                            if(data.code == 'S_000') {
                                myPub.TipLayer(data.msg);
                                var lastUrl = getCookie("historyLastUrl");
                                if(lastUrl !=undefined && lastUrl.length>0){
                                    var url = lastUrl.replace(/"/g,'');
                                    window.location.href=url;
                                }else{
                                    window.location.href="/home";
                                }
                            }else if(data.code == 'S_111'){
                                window.location.href=data.msg;
                            } else {
                                myPub.TipLayer(data.msg);
                            }
                        },
                        error:function(x){
                            if(403==x.status){
                                window.location.href="/loginExpired";
                            }
                            console.log(x.status);
                        }
                    });
                })
            }
        }
    });

    //注册操作
    var oRegSubmit = $('.js_regSubmit');

    oRegSubmit.hammer().on('tap', function() {
        //检测是否可以点击
        if(oAgrSubmit.hasClass('act_Disabled')) {
            myPub.TipLayer('必须同意协议才能进行注册');
            return
        }

        SublimtAfter (function () {
            $.ajax({
                url: '/ddjLoginAuth/register',
                data: {
                    "mobile": $('.js_loginPhoneNum').val(),
                    "password":$('#password').val(),
                    "smsCode" : $('.js_zhuceCode').val()
                },
                type: 'POST',
                dataType: 'json',
                success: function(data) {
                    if(data.code == 'S_000') {
                        myPub.TipLayer(data.msg);
                        var lastUrl = getCookie("historyLastUrl");
                        if(lastUrl !=undefined && lastUrl.length>0){
                            var url = lastUrl.replace(/"/g,'');
                            window.location.href=url;
                        }else{
                            window.location.href="/home";
                        }
                    } else {
                        myPub.TipLayer(data.msg);
                    }
                },
                error:function(x){
                    if(403==x.status){
                        window.location.href="/loginExpired";
                    }
                    console.log(x.status);
                }
            });
        })

    });

    /*********************  协议点击是否同意设置  ****************************/
    var oAgreementInfo = $('.js_agreementInfo');
    var oAgrSubmit = $('.js_agrSubmit')
    oAgreementInfo.hammer().on('tap', function() {
        if(oAgreementInfo.hasClass('active')) {
            oAgreementInfo.removeClass('active');
            oAgrSubmit.addClass('act_Disabled')
        }
        else {
            oAgreementInfo.addClass('active');
            oAgrSubmit.removeClass('act_Disabled')
        }
        myPub.HamstopPropaga();
    });



    //绑定操作
    var oBindSubmit = $('.js_bindSubmit')

    oBindSubmit.hammer().on('tap', function() {
        //检测是否可以点击
        if(oAgrSubmit.hasClass('act_Disabled')) {
            myPub.TipLayer('必须同意协议才能进行注册');
            return
        };

        SublimtAfter (function () {
            var phonev = $("#phone").val();
            var verCode = $("#verCode").val();
            var pwd = $("#password").val();
            /** 页面上所有异步请求全部调用G_JSON_POST方法 */
            G_JSON_POST('/member/bing',{"mobile":phonev,"verCode":verCode,"password":pwd},function(data){
                if(data.status == "success"){
                    myPub.TipLayerEnd("绑定成功",'',function(){
                        //window.location.href = baseurl;
                        //window.location.href = 'https://m.sfddj.com/member/myaccount';
                        if(backHref != ""){
                            window.location.href = backHref;
                        }else{
                            window.location.href = "/";
                        }
                    },'');
                }else{
                    myPub.TipLayer(data.msg);
                }
            });
        })
    });



    //找回密码
    var oFindSubmit = $('.js_findSubmit');
    var oUserAsk = $('.js_userAsk');
    var oClearBtnAsk = oUserAsk.siblings('.js_clearBtn');
    oUserAsk.on('input propertychange',function () {
        $(this).val() == '' ? oClearBtnAsk.hide() : oClearBtnAsk.show();
    })
    oClearBtnAsk.hammer().on('tap', function() {oUserAsk.val(''); })


    //提交找回密码
    oFindSubmit.hammer().on('tap', function() {
        SublimtAfter (function () {
            if($("#userAsk").val() == '' && ischeckAnwser) {
                myPub.TipLayer('请输入你选择的答案！');
            }else {
                $.ajax({
                    url: '/ddjLoginAuth/retrievePassword',
                    data: {
                        "mobile": $('.js_loginPhoneNum').val(),
                        "password":$('#password').val(),
                        "smsCode" : $('.js_zhCode').val(),
                        "userAnswer":$("#userAnswer").val(),
                        "userAsk":$("#userAsk").val()
                    },
                    type: 'POST',
                    dataType: 'json',
                    success: function(data) {
                        if(data.code == 'S_000') {
                            myPub.TipLayer(data.msg);
                            var lastUrl = getCookie("historyLastUrl");
                            if(lastUrl !=undefined && lastUrl.length>0){
                                var url = lastUrl.replace(/"/g,'');
                                window.location.href=url;
                            }else{
                                window.location.href="/home";
                            }
                        } else {
                            refreshCode();
                            myPub.TipLayer(data.msg);
                        }
                    },
                    error:function(x){
                        if(403==x.status){
                            window.location.href="/loginExpired";
                        }
                        console.log(x.status);
                    }
                });
            }


        })
    });


    //提交表单前操作 检验格式
    //fnEmpty:输入框有未填     fnError:错误格式   fnRight：符合格式
    function SublimtBefor (fnEmpty, fnError, fnRight) {
        var _offBtn = 0;
        var _iNow;
        var oInputBoxList = $('.js_inputBoxList');

        for(var i = 0; i < oInputBoxList.find('.js_Text').length; i++) {
            if(oInputBoxList.find('.js_Text').eq(i).val() == '') {
                _offBtn++
                _iNow = i
                break;
            }
        }

        //没有空密码
        if(_offBtn == 0) {
            //错格式密码提示
            if(oInputBoxList.find('.has_error').length > 0) {
                var _errorNum = oInputBoxList.find('.js_login_input').index(oInputBoxList.find('.has_error'))
                //格式不正确
                if(fnError) fnError(_errorNum)
                //myPub.TipLayer('请输入6-20位数字或者英文字母的密码格式');
            } else {
                //没有错误
                if(fnRight) fnRight()
            }

        }
        //有空密码
        else {
            if(fnEmpty) fnEmpty(_iNow);
            //myPub.TipLayer('密码不能为空');
        }

    }

    //前端效验完成
    function SublimtAfter (callBack) {
        if(offPicYzm) {
            //图形验证码隐藏
            SublimtBefor(function(index) {
                switch(index) {
                    case 0:
                        myPub.TipLayer('手机号码不能为空');
                        break;
                    case 1:
                        myPub.TipLayer('动态验证码不能为空');
                        break;
                    case 2:
                        myPub.TipLayer('请输入密码');
                        break;

                }
            }, function(index) {
                switch(index) {
                    case 0:
                        myPub.TipLayer('请输入正确的手机号码');
                        break;
                    case 1:
                        myPub.TipLayer('请输入正确的动态验证码');
                        break;
                    case 2:
                        myPub.TipLayer('请输入正确的密码');
                        break;

                }

            }, function() {
                if(callBack)callBack()
            });
        }
        else {
            //图形验证码显示
            SublimtBefor(function(index) {
                switch(index) {
                    case 0:
                        myPub.TipLayer('手机号码不能为空');
                        break;
                    case 1:
                        myPub.TipLayer('请输入右边验证码');
                        break;
                    case 2:
                        myPub.TipLayer('动态验证码不能为空');
                        break;
                    case 3:
                        myPub.TipLayer('请输入密码');
                        break;
                }
            }, function(index) {
                switch(index) {
                    case 0:
                        myPub.TipLayer('请输入正确的手机号码');
                        break;
                    case 1:
                        myPub.TipLayer('请输入正确的验证码');
                        break;
                    case 2:
                        myPub.TipLayer('请输入正确的动态验证码');
                        break;
                    case 3:
                        myPub.TipLayer('请输入正确格式的密码');
                        break;
                }

            }, function() {
                if(callBack)callBack()
            })

        }

    }

    function getCookie(name)
    {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    }

});

function Encrypt(word){
    var strInfo = "0102030405060708";
    var key = CryptoJS.enc.Utf8.parse(strInfo);
    var iv  = CryptoJS.enc.Utf8.parse(strInfo);
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC});
    return encrypted.toString();
}